#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void add ( int* dia, int i, int D, int* tmp );
// void showarray ( int* dia, int i );
void queue ( int* arr, int len );

int main() {
    int P, D, num = 2;
    int* dia;
    puts("input diamond number and pirate number: ");
    scanf("%d %d",&D,&P);
    dia = (int*)malloc(P*sizeof(int));
    memset(dia, 0, P);
    int* tmp;
    tmp = (int*)malloc(P*sizeof(int));
    
    // for ( num = 0; num < P; num++ ) dia[num] = 0;
    dia[0] = D;
    for ( num = 2; num < P; num++ ) {
        add(dia, num, D, tmp);
        // showarray(dia, num+1);
    }
    printf("%d",dia[P-1]);
    free(dia);
        free(tmp);
    return 0;
}
// void showarray ( int* dia, int len) {
//     for (int cnt = 0; cnt < len; cnt++ ) {
//         printf("%d ",dia[cnt]);
//     }
//     printf("\n");
// }
void add ( int* dia, int len, int D, int* tmp ) {
    int cnt = 0, plus = 0, sum = 0;
  for ( cnt = 0; cnt < len; cnt++ ) {
        tmp[cnt] = dia[cnt];
    }
    // sort
    queue(tmp,len);
    // 
    int mid = tmp[(len-1)/2];
    for ( cnt = 0; cnt < len; cnt++ ) {
        if ( dia[cnt] > mid ) dia[cnt] = 0;
        else {
            dia[cnt]++;
            plus++;
            sum+=dia[cnt];
        }
    }
    // check plus number
    for ( cnt = 0; plus > ((len+1)/2); cnt++ ) {
        if ( dia[cnt] == mid +1 ) {
            dia[cnt] = 0;
            plus--;
            sum-=( mid + 1 );
        }
    }
    dia[len] = D - sum;

}
// sort array
void queue ( int* arr, int len ) {
    int i, j, tmp;
    for ( i = 0; i < len; i++ ) {
        tmp = arr[i];
        for ( j = i + 1; j < len; j++ ) {
            if ( arr[j] < tmp ) {
                arr[i] = arr[j];
                arr[j] = tmp;
            }
        }
    }
}